/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package traffic;

import com.google.code.eventsonfire.EventHandler;
import comunicacao.GestorZooKeeper;
import entities.Server;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;

/**
 *
 * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class ControladorTrafego {

    public static Server selectedServer;
    public static int targetTeatroFirst = 0;
    public static int targetTeatroLast = 0;
    public static boolean randomClients = false;
    public static int clientID;
    public static int numTeatros;
    public static int numRows;
    public static int numCols;

    public static ArrayList<Server> servers;

    public static final int LOAD_TYPE_CONSTANT = 0;
    public static final int LOAD_TYPE_PROGRESSIVE = 1;
    private int loadType;

    private Configuracao config;
    GeradorPanel geradorPainel;

    private GestorZooKeeper gestorZK;

    public ControladorTrafego() {
        loadZooKeeperServers();
    }

    private void loadZooKeeperServers() {
        gestorZK = new GestorZooKeeper();
        servers = gestorZK.getAllServers();

        ArrayList<String> varios = gestorZK.getNode("/AppServersConfig/NumTheaters");
        numTeatros = Integer.parseInt(varios.get(0));
        numRows = Integer.parseInt(varios.get(1));
        numCols = Integer.parseInt(varios.get(2));
    }

    public void start() {
        launchGestorThreads();
        String typeClient = getSelectedButtonText(config.buttonGroup1);

        if (typeClient.equals("Single Client")) {
            Random rand = new Random();
            clientID = rand.nextInt(100000);
            randomClients = false;
        } else {
            randomClients = true;
        }

        targetTeatroFirst = Integer.parseInt(config.textFieldFirstTheater.getText().toString());
        if (config.comboBoxTargetThreater.getSelectedIndex() == 0) {
            targetTeatroLast = Integer.parseInt(config.textFieldLastTheater.getText().toString());
        } else {
            targetTeatroLast = 0;
        }
    }

    private void launchGestorThreads() {
        int pedidos = geradorPainel.pedidosSlider.getValue();
        int segundos = Integer.parseInt(geradorPainel.segundosField.getText());
        int numThreads = Integer.parseInt(config.numThreadsField.getText());

        if (geradorPainel.comboServer.getSelectedIndex() == 1) {
            Server db = new Server("1", 1);
            db.setNodeName("DBPrimary");
            db.setType(Server.TYPE_DB);
            selectedServer = db;
        } else {
            selectedServer = servers.get(0);
        }

        Runnable task = new GestorThreads(pedidos, segundos, numThreads,
                config.checkListaTeatros.isSelected(), config.checkTeatro.isSelected(), config.checkPurchase.isSelected());

        Thread worker = new Thread(task);
        worker.setName("Gestor Threads");
        worker.start();

    }

    public String getSelectedButtonText(ButtonGroup buttonGroup) {
        for (Enumeration<AbstractButton> buttons = buttonGroup.getElements(); buttons.hasMoreElements();) {
            AbstractButton button = buttons.nextElement();

            if (button.isSelected()) {
                return button.getText();
            }
        }

        return null;
    }

    public void setConfig(Configuracao config) {
        this.config = config;

    }

    public void setGeradorPainel(GeradorPanel geradorPainel) {
        this.geradorPainel = geradorPainel;
        System.out.println("set gerador");

        int load = geradorPainel.comboType.getSelectedIndex();
        loadType = load;
    }

    public ArrayList<Server> getServers() {
        return servers;
    }

}
